{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Pre-processing:\n",
    "## Generating config files"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np \n",
    "import pandas as pd\n",
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "#connect to the server and copy the path to your sequencing data\n",
    "fp = '/Volumes/sanchezrivera/samgould/240624San/'\n",
    "k = os.listdir(fp)\n",
    "\n",
    "#might need to change the 'D24' depending on file naming\n",
    "k = [i for i in k if i[:3]=='D24'] \n",
    "\n",
    "folder_name = []\n",
    "f1 = []\n",
    "f2 = []\n",
    "for ii in range(len(k)):\n",
    "    j = os.listdir(f'{fp}/{k[ii]}')\n",
    "    j = sorted([i for i in j if i[-5:]=='fastq'])\n",
    "\n",
    "    #weird error to deal with\n",
    "    #hacky solution\n",
    "    if len(j)==2:\n",
    "        f1.append(f'./{k[ii]}/{j[0]}')\n",
    "        f2.append(f'./{k[ii]}/{j[1]}')\n",
    "    else:\n",
    "        f1.append(f'./{k[ii]}/{j[2]}')\n",
    "        f2.append(f'./{k[ii]}/{j[3]}')\n",
    "    folder_name.append(k[ii] + '_guide_split_BARCODES')\n",
    "\n",
    "config_df = pd.DataFrame(dict(zip(['ArrayTaskID', 'R1_FILE', 'R2_FILE', 'folder_name'], [list(range(1, len(f1)+1)), f1, f2, folder_name])))\n",
    "#config_df.to_csv('sensor_extraction_fastq_split_config_DIEGO_IDR.txt', index=False, sep = ' ')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ArrayTaskID</th>\n",
       "      <th>R1_FILE</th>\n",
       "      <th>R2_FILE</th>\n",
       "      <th>folder_name</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>./D24-10888-6775T/240624San_D24-10888_1_sequen...</td>\n",
       "      <td>./D24-10888-6775T/240624San_D24-10888_2_sequen...</td>\n",
       "      <td>D24-10888-6775T_guide_split_BARCODES</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>./D24-10889-6775T/240624San_D24-10889_1_sequen...</td>\n",
       "      <td>./D24-10889-6775T/240624San_D24-10889_2_sequen...</td>\n",
       "      <td>D24-10889-6775T_guide_split_BARCODES</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>./D24-10890-6775T/240624San_D24-10890_1_sequen...</td>\n",
       "      <td>./D24-10890-6775T/240624San_D24-10890_2_sequen...</td>\n",
       "      <td>D24-10890-6775T_guide_split_BARCODES</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>./D24-10891-6775T/240624San_D24-10891_1_sequen...</td>\n",
       "      <td>./D24-10891-6775T/240624San_D24-10891_2_sequen...</td>\n",
       "      <td>D24-10891-6775T_guide_split_BARCODES</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>./D24-10892-6775T/240624San_D24-10892_1_sequen...</td>\n",
       "      <td>./D24-10892-6775T/240624San_D24-10892_2_sequen...</td>\n",
       "      <td>D24-10892-6775T_guide_split_BARCODES</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>6</td>\n",
       "      <td>./D24-10893-6775T/240624San_D24-10893_1_sequen...</td>\n",
       "      <td>./D24-10893-6775T/240624San_D24-10893_2_sequen...</td>\n",
       "      <td>D24-10893-6775T_guide_split_BARCODES</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   ArrayTaskID                                            R1_FILE  \\\n",
       "0            1  ./D24-10888-6775T/240624San_D24-10888_1_sequen...   \n",
       "1            2  ./D24-10889-6775T/240624San_D24-10889_1_sequen...   \n",
       "2            3  ./D24-10890-6775T/240624San_D24-10890_1_sequen...   \n",
       "3            4  ./D24-10891-6775T/240624San_D24-10891_1_sequen...   \n",
       "4            5  ./D24-10892-6775T/240624San_D24-10892_1_sequen...   \n",
       "5            6  ./D24-10893-6775T/240624San_D24-10893_1_sequen...   \n",
       "\n",
       "                                             R2_FILE  \\\n",
       "0  ./D24-10888-6775T/240624San_D24-10888_2_sequen...   \n",
       "1  ./D24-10889-6775T/240624San_D24-10889_2_sequen...   \n",
       "2  ./D24-10890-6775T/240624San_D24-10890_2_sequen...   \n",
       "3  ./D24-10891-6775T/240624San_D24-10891_2_sequen...   \n",
       "4  ./D24-10892-6775T/240624San_D24-10892_2_sequen...   \n",
       "5  ./D24-10893-6775T/240624San_D24-10893_2_sequen...   \n",
       "\n",
       "                            folder_name  \n",
       "0  D24-10888-6775T_guide_split_BARCODES  \n",
       "1  D24-10889-6775T_guide_split_BARCODES  \n",
       "2  D24-10890-6775T_guide_split_BARCODES  \n",
       "3  D24-10891-6775T_guide_split_BARCODES  \n",
       "4  D24-10892-6775T_guide_split_BARCODES  \n",
       "5  D24-10893-6775T_guide_split_BARCODES  "
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "config_df"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Example post-processing"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def crispresso_compiler(samp, sample_id, fp):\n",
    "    \"\"\" \n",
    "    Takes in list of sample names (see cell above)\n",
    "    Returns compiled dictionary of dataframes containing editing information for each sensor\n",
    "    - key for each is the sample_id name\n",
    "    \n",
    "    Includes:\n",
    "    1. corr_perc = pure correct editing\n",
    "    2. target_base_edit_perc = target base editing perc (including edits with bystander editing)\n",
    "    3. wt_perc\n",
    "    4. byproduct information (indels, substitutions, ambiguous)\n",
    "    \"\"\"\n",
    "\n",
    "    df_edits = []\n",
    "    for k in samp: \n",
    "        concated = pd.read_csv(f'{fp}/{k}_crispresso_aggregated.csv')\n",
    "        concated = concated.fillna(0)\n",
    "\n",
    "        #somehow wasn't able to find the initial set of code that I used for this...\n",
    "\n",
    "        #go through all of the samples and do it step by step\n",
    "        sample_ids = []\n",
    "        #sample_num = []\n",
    "        rii = []\n",
    "        raaa = []\n",
    "        r_lowqual = []\n",
    "        ra_hdr = []\n",
    "        ra_wt = []\n",
    "        r_unaligned = []\n",
    "        no_edit = []\n",
    "        correct = []\n",
    "        target_base_editing = []\n",
    "        byproduct_all = []\n",
    "        byproduct_indel = []\n",
    "        byproduct_sub = []\n",
    "        byproduct_ambig = []\n",
    "        for i in np.unique(concated['Guide_ID']):\n",
    "            sample_ids.append(i)\n",
    "\n",
    "            subset = concated[concated['Guide_ID']==i]\n",
    "\n",
    "            wt = subset[subset['Amplicon']=='Reference']\n",
    "            edit = subset[subset['Amplicon']=='HDR']\n",
    "\n",
    "            #sample_num.append(wt['sample_num'].values[0])\n",
    "            rii1 = wt['Reads_in_input'].values[0]\n",
    "            r1 = wt['Reads_aligned_all_amplicons'].values[0]\n",
    "            rii.append(rii1)\n",
    "            raaa.append(r1)\n",
    "            r_lowqual.append(rii1-r1)\n",
    "\n",
    "            r2 = edit['Reads_aligned'].values[0]\n",
    "            r3 = wt['Reads_aligned'].values[0]\n",
    "            ra_hdr.append(r2)\n",
    "            ra_wt.append(r3)\n",
    "            r_unaligned.append(r1 - (r2+r3))\n",
    "\n",
    "            no_edit.append(wt['Unmodified'].values[0])\n",
    "            correct.append(edit['Unmodified'].values[0])\n",
    "            target_base_editing.append(edit['Modified'].values[0] + edit['Unmodified'].values[0])\n",
    "\n",
    "            byprod_all = wt['Modified'].values[0] + edit['Modified'].values[0] + (r1 - (r2+r3)) #add unaligned reads\n",
    "            sub_all = wt['Only Substitutions'].values[0] + edit['Only Substitutions'].values[0]\n",
    "            indel_all = wt['Only Deletions'].values[0] + wt['Only Insertions'].values[0] + wt['Insertions and Deletions'].values[0] + edit['Only Deletions'].values[0] + edit['Only Insertions'].values[0] + edit['Insertions and Deletions'].values[0]\n",
    "            ambig_all = byprod_all - sub_all - indel_all\n",
    "\n",
    "            byproduct_all.append(byprod_all)\n",
    "            byproduct_indel.append(indel_all)\n",
    "            byproduct_sub.append(sub_all)\n",
    "            byproduct_ambig.append(ambig_all)\n",
    "\n",
    "\n",
    "        cols = [\"Guide_ID\", \"Reads_in_input\",\"Reads_lowqual\", \"Reads_aligned_all_amplicons\",\"Reads_aligned_WT\", \"Reads_aligned_HDR\", \"Reads_unaligned\", \"WT\",\"correct_edit\", \"target_base_edit\", \"byproduct_all\",\"byproduct_INDEL\",\"byproduct_sub\",\"byproduct_ambiguous\"]\n",
    "        col_vals = [ sample_ids, rii, r_lowqual, raaa, ra_wt, ra_hdr, r_unaligned, no_edit, correct, target_base_editing, byproduct_all, byproduct_indel, byproduct_sub, byproduct_ambig]\n",
    "\n",
    "        out = pd.DataFrame(dict(zip(cols, col_vals)))\n",
    "        out['corr_perc'] = 100*(out['correct_edit']/out['Reads_aligned_all_amplicons'])\n",
    "        out['target_base_edit_perc'] = 100*(out['target_base_edit']/out['Reads_aligned_all_amplicons'])\n",
    "        out['WT_perc'] = 100*(out['WT']/out['Reads_aligned_all_amplicons'])\n",
    "        out['byproduct_all_perc'] =  100*(out['byproduct_all']/out['Reads_aligned_all_amplicons'])\n",
    "        out['byproduct_INDEL_perc'] =  100*(out['byproduct_INDEL']/out['Reads_aligned_all_amplicons'])\n",
    "        out['byproduct_sub_perc'] =  100*(out['byproduct_sub']/out['Reads_aligned_all_amplicons'])\n",
    "        out['byproduct_ambiguous_perc'] =100*(out['byproduct_ambiguous']/out['Reads_aligned_all_amplicons'])\n",
    "        out = out.fillna(0)\n",
    "\n",
    "        df_edits.append(out)\n",
    "\n",
    "    edit_dict = dict(zip(sample_id, df_edits))\n",
    "\n",
    "    return edit_dict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['D24-10888-6775T_MISMATCH_PIPELINE',\n",
       " 'D24-10889-6775T_MISMATCH_PIPELINE',\n",
       " 'D24-10890-6775T_MISMATCH_PIPELINE',\n",
       " 'D24-10891-6775T_MISMATCH_PIPELINE',\n",
       " 'D24-10892-6775T_MISMATCH_PIPELINE',\n",
       " 'D24-10893-6775T_MISMATCH_PIPELINE']"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "config = pd.read_csv('example_config_and_library_file/DIEGO_IDR_CONFIG_MISMATCH_PIPELINE.txt', sep=' ')\n",
    "list(config['folder_name'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "#example_inputs\n",
    "samp = ['D24-10888-6775T_MISMATCH_PIPELINE',\n",
    " 'D24-10889-6775T_MISMATCH_PIPELINE',\n",
    " 'D24-10890-6775T_MISMATCH_PIPELINE',\n",
    " 'D24-10891-6775T_MISMATCH_PIPELINE',\n",
    " 'D24-10892-6775T_MISMATCH_PIPELINE',\n",
    " 'D24-10893-6775T_MISMATCH_PIPELINE']\n",
    "\n",
    "sample_id = ['sample1', 'sample2', 'sample3', 'sample4', 'sample5', 'sample6']\n",
    "\n",
    "#filepath for crispresso data\n",
    "fp = 'example_data/crispresso'\n",
    "\n",
    "edit_dict = crispresso_compiler(samp, sample_id, fp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Guide_ID</th>\n",
       "      <th>Reads_in_input</th>\n",
       "      <th>Reads_lowqual</th>\n",
       "      <th>Reads_aligned_all_amplicons</th>\n",
       "      <th>Reads_aligned_WT</th>\n",
       "      <th>Reads_aligned_HDR</th>\n",
       "      <th>Reads_unaligned</th>\n",
       "      <th>WT</th>\n",
       "      <th>correct_edit</th>\n",
       "      <th>target_base_edit</th>\n",
       "      <th>...</th>\n",
       "      <th>byproduct_INDEL</th>\n",
       "      <th>byproduct_sub</th>\n",
       "      <th>byproduct_ambiguous</th>\n",
       "      <th>corr_perc</th>\n",
       "      <th>target_base_edit_perc</th>\n",
       "      <th>WT_perc</th>\n",
       "      <th>byproduct_all_perc</th>\n",
       "      <th>byproduct_INDEL_perc</th>\n",
       "      <th>byproduct_sub_perc</th>\n",
       "      <th>byproduct_ambiguous_perc</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>sg_1</td>\n",
       "      <td>1224</td>\n",
       "      <td>0</td>\n",
       "      <td>1224</td>\n",
       "      <td>33</td>\n",
       "      <td>1189</td>\n",
       "      <td>2</td>\n",
       "      <td>25</td>\n",
       "      <td>99</td>\n",
       "      <td>1189</td>\n",
       "      <td>...</td>\n",
       "      <td>44</td>\n",
       "      <td>1033</td>\n",
       "      <td>23</td>\n",
       "      <td>8.088235</td>\n",
       "      <td>97.140523</td>\n",
       "      <td>2.042484</td>\n",
       "      <td>89.869281</td>\n",
       "      <td>3.594771</td>\n",
       "      <td>84.395425</td>\n",
       "      <td>1.879085</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>sg_10</td>\n",
       "      <td>1694</td>\n",
       "      <td>0</td>\n",
       "      <td>1694</td>\n",
       "      <td>846</td>\n",
       "      <td>847</td>\n",
       "      <td>1</td>\n",
       "      <td>16</td>\n",
       "      <td>0</td>\n",
       "      <td>847</td>\n",
       "      <td>...</td>\n",
       "      <td>32</td>\n",
       "      <td>1623</td>\n",
       "      <td>23</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>50.000000</td>\n",
       "      <td>0.944510</td>\n",
       "      <td>99.055490</td>\n",
       "      <td>1.889020</td>\n",
       "      <td>95.808737</td>\n",
       "      <td>1.357733</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>sg_100</td>\n",
       "      <td>581</td>\n",
       "      <td>0</td>\n",
       "      <td>581</td>\n",
       "      <td>5</td>\n",
       "      <td>564</td>\n",
       "      <td>12</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>564</td>\n",
       "      <td>...</td>\n",
       "      <td>4</td>\n",
       "      <td>550</td>\n",
       "      <td>26</td>\n",
       "      <td>0.172117</td>\n",
       "      <td>97.074010</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>99.827883</td>\n",
       "      <td>0.688468</td>\n",
       "      <td>94.664372</td>\n",
       "      <td>4.475043</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>sg_1000</td>\n",
       "      <td>271</td>\n",
       "      <td>8</td>\n",
       "      <td>263</td>\n",
       "      <td>32</td>\n",
       "      <td>226</td>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>0</td>\n",
       "      <td>226</td>\n",
       "      <td>...</td>\n",
       "      <td>57</td>\n",
       "      <td>162</td>\n",
       "      <td>38</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>85.931559</td>\n",
       "      <td>2.281369</td>\n",
       "      <td>97.718631</td>\n",
       "      <td>21.673004</td>\n",
       "      <td>61.596958</td>\n",
       "      <td>14.448669</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>sg_1001</td>\n",
       "      <td>1104</td>\n",
       "      <td>20</td>\n",
       "      <td>1084</td>\n",
       "      <td>23</td>\n",
       "      <td>1039</td>\n",
       "      <td>22</td>\n",
       "      <td>18</td>\n",
       "      <td>72</td>\n",
       "      <td>1039</td>\n",
       "      <td>...</td>\n",
       "      <td>46</td>\n",
       "      <td>881</td>\n",
       "      <td>67</td>\n",
       "      <td>6.642066</td>\n",
       "      <td>95.848708</td>\n",
       "      <td>1.660517</td>\n",
       "      <td>91.697417</td>\n",
       "      <td>4.243542</td>\n",
       "      <td>81.273063</td>\n",
       "      <td>6.180812</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1667</th>\n",
       "      <td>sg_995</td>\n",
       "      <td>890</td>\n",
       "      <td>46</td>\n",
       "      <td>844</td>\n",
       "      <td>56</td>\n",
       "      <td>772</td>\n",
       "      <td>16</td>\n",
       "      <td>18</td>\n",
       "      <td>94</td>\n",
       "      <td>772</td>\n",
       "      <td>...</td>\n",
       "      <td>48</td>\n",
       "      <td>605</td>\n",
       "      <td>79</td>\n",
       "      <td>11.137441</td>\n",
       "      <td>91.469194</td>\n",
       "      <td>2.132701</td>\n",
       "      <td>86.729858</td>\n",
       "      <td>5.687204</td>\n",
       "      <td>71.682464</td>\n",
       "      <td>9.360190</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1668</th>\n",
       "      <td>sg_996</td>\n",
       "      <td>2071</td>\n",
       "      <td>14</td>\n",
       "      <td>2057</td>\n",
       "      <td>66</td>\n",
       "      <td>1970</td>\n",
       "      <td>21</td>\n",
       "      <td>29</td>\n",
       "      <td>0</td>\n",
       "      <td>1970</td>\n",
       "      <td>...</td>\n",
       "      <td>2</td>\n",
       "      <td>1947</td>\n",
       "      <td>79</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>95.770540</td>\n",
       "      <td>1.409820</td>\n",
       "      <td>98.590180</td>\n",
       "      <td>0.097229</td>\n",
       "      <td>94.652406</td>\n",
       "      <td>3.840544</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1669</th>\n",
       "      <td>sg_997</td>\n",
       "      <td>1835</td>\n",
       "      <td>1</td>\n",
       "      <td>1834</td>\n",
       "      <td>995</td>\n",
       "      <td>837</td>\n",
       "      <td>2</td>\n",
       "      <td>111</td>\n",
       "      <td>25</td>\n",
       "      <td>837</td>\n",
       "      <td>...</td>\n",
       "      <td>9</td>\n",
       "      <td>1657</td>\n",
       "      <td>32</td>\n",
       "      <td>1.363141</td>\n",
       "      <td>45.637950</td>\n",
       "      <td>6.052345</td>\n",
       "      <td>92.584515</td>\n",
       "      <td>0.490731</td>\n",
       "      <td>90.348964</td>\n",
       "      <td>1.744820</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1670</th>\n",
       "      <td>sg_998</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>100.000000</td>\n",
       "      <td>100.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1671</th>\n",
       "      <td>sg_999</td>\n",
       "      <td>1295</td>\n",
       "      <td>5</td>\n",
       "      <td>1290</td>\n",
       "      <td>401</td>\n",
       "      <td>853</td>\n",
       "      <td>36</td>\n",
       "      <td>28</td>\n",
       "      <td>0</td>\n",
       "      <td>853</td>\n",
       "      <td>...</td>\n",
       "      <td>4</td>\n",
       "      <td>1206</td>\n",
       "      <td>52</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>66.124031</td>\n",
       "      <td>2.170543</td>\n",
       "      <td>97.829457</td>\n",
       "      <td>0.310078</td>\n",
       "      <td>93.488372</td>\n",
       "      <td>4.031008</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1672 rows × 21 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     Guide_ID  Reads_in_input  Reads_lowqual  Reads_aligned_all_amplicons  \\\n",
       "0        sg_1            1224              0                         1224   \n",
       "1       sg_10            1694              0                         1694   \n",
       "2      sg_100             581              0                          581   \n",
       "3     sg_1000             271              8                          263   \n",
       "4     sg_1001            1104             20                         1084   \n",
       "...       ...             ...            ...                          ...   \n",
       "1667   sg_995             890             46                          844   \n",
       "1668   sg_996            2071             14                         2057   \n",
       "1669   sg_997            1835              1                         1834   \n",
       "1670   sg_998               4              0                            4   \n",
       "1671   sg_999            1295              5                         1290   \n",
       "\n",
       "      Reads_aligned_WT  Reads_aligned_HDR  Reads_unaligned   WT  correct_edit  \\\n",
       "0                   33               1189                2   25            99   \n",
       "1                  846                847                1   16             0   \n",
       "2                    5                564               12    0             1   \n",
       "3                   32                226                5    6             0   \n",
       "4                   23               1039               22   18            72   \n",
       "...                ...                ...              ...  ...           ...   \n",
       "1667                56                772               16   18            94   \n",
       "1668                66               1970               21   29             0   \n",
       "1669               995                837                2  111            25   \n",
       "1670                 0                  4                0    0             4   \n",
       "1671               401                853               36   28             0   \n",
       "\n",
       "      target_base_edit  ...  byproduct_INDEL  byproduct_sub  \\\n",
       "0                 1189  ...               44           1033   \n",
       "1                  847  ...               32           1623   \n",
       "2                  564  ...                4            550   \n",
       "3                  226  ...               57            162   \n",
       "4                 1039  ...               46            881   \n",
       "...                ...  ...              ...            ...   \n",
       "1667               772  ...               48            605   \n",
       "1668              1970  ...                2           1947   \n",
       "1669               837  ...                9           1657   \n",
       "1670                 4  ...                0              0   \n",
       "1671               853  ...                4           1206   \n",
       "\n",
       "      byproduct_ambiguous   corr_perc  target_base_edit_perc   WT_perc  \\\n",
       "0                      23    8.088235              97.140523  2.042484   \n",
       "1                      23    0.000000              50.000000  0.944510   \n",
       "2                      26    0.172117              97.074010  0.000000   \n",
       "3                      38    0.000000              85.931559  2.281369   \n",
       "4                      67    6.642066              95.848708  1.660517   \n",
       "...                   ...         ...                    ...       ...   \n",
       "1667                   79   11.137441              91.469194  2.132701   \n",
       "1668                   79    0.000000              95.770540  1.409820   \n",
       "1669                   32    1.363141              45.637950  6.052345   \n",
       "1670                    0  100.000000             100.000000  0.000000   \n",
       "1671                   52    0.000000              66.124031  2.170543   \n",
       "\n",
       "      byproduct_all_perc  byproduct_INDEL_perc  byproduct_sub_perc  \\\n",
       "0              89.869281              3.594771           84.395425   \n",
       "1              99.055490              1.889020           95.808737   \n",
       "2              99.827883              0.688468           94.664372   \n",
       "3              97.718631             21.673004           61.596958   \n",
       "4              91.697417              4.243542           81.273063   \n",
       "...                  ...                   ...                 ...   \n",
       "1667           86.729858              5.687204           71.682464   \n",
       "1668           98.590180              0.097229           94.652406   \n",
       "1669           92.584515              0.490731           90.348964   \n",
       "1670            0.000000              0.000000            0.000000   \n",
       "1671           97.829457              0.310078           93.488372   \n",
       "\n",
       "      byproduct_ambiguous_perc  \n",
       "0                     1.879085  \n",
       "1                     1.357733  \n",
       "2                     4.475043  \n",
       "3                    14.448669  \n",
       "4                     6.180812  \n",
       "...                        ...  \n",
       "1667                  9.360190  \n",
       "1668                  3.840544  \n",
       "1669                  1.744820  \n",
       "1670                  0.000000  \n",
       "1671                  4.031008  \n",
       "\n",
       "[1672 rows x 21 columns]"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#can then access editing through \n",
    "edit_dict['sample1']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Merging sensor editing results\n",
    "\n",
    "It's often useful to merge sensor editing results from the same time-point to make a maximum likelihood estimate of sensor editing:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "def MLE_merge(edit_dict, samps_to_merge, new_name):\n",
    "    \"\"\" \n",
    "    combine replicates to generate best estimate of sensor editing\n",
    "    only works for combinations of 3 replicates (could be modified)\n",
    "    \"\"\"\n",
    "    cols_to_add = ['Reads_in_input', 'Reads_lowqual',\n",
    "       'Reads_aligned_all_amplicons', 'Reads_aligned_WT', 'Reads_aligned_HDR',\n",
    "       'Reads_unaligned', 'WT', 'correct_edit', 'target_base_edit',\n",
    "       'byproduct_all', 'byproduct_INDEL', 'byproduct_sub',\n",
    "       'byproduct_ambiguous',]\n",
    "    \n",
    "    comb_holder = []\n",
    "    for k in samps_to_merge:\n",
    "        a = edit_dict[k[0]]\n",
    "        b = edit_dict[k[1]]\n",
    "        c = edit_dict[k[2]]\n",
    "\n",
    "        #check that guide indeces match\n",
    "        assert list(a['Guide_ID'])==list(b['Guide_ID'])==list(c['Guide_ID']), 'indexes dont match'\n",
    "\n",
    "        a2 = a[cols_to_add]\n",
    "        b2 = b[cols_to_add]\n",
    "        c2 = c[cols_to_add]\n",
    "\n",
    "        comb = a2+b2+c2\n",
    "\n",
    "        comb['Guide_ID'] = a['Guide_ID']\n",
    "\n",
    "        #and reorganize columns\n",
    "        col_order = ['Guide_ID'] + cols_to_add\n",
    "        out = comb[col_order]\n",
    "\n",
    "        #and then calcualte percentages\n",
    "        out['corr_perc'] = 100*(out['correct_edit']/out['Reads_aligned_all_amplicons'])\n",
    "        out['target_base_edit_perc'] = 100*(out['target_base_edit']/out['Reads_aligned_all_amplicons'])\n",
    "        out['WT_perc'] = 100*(out['WT']/out['Reads_aligned_all_amplicons'])\n",
    "        out['byproduct_all_perc'] =  100*(out['byproduct_all']/out['Reads_aligned_all_amplicons'])\n",
    "        out['byproduct_INDEL_perc'] =  100*(out['byproduct_INDEL']/out['Reads_aligned_all_amplicons'])\n",
    "        out['byproduct_sub_perc'] =  100*(out['byproduct_sub']/out['Reads_aligned_all_amplicons'])\n",
    "        out['byproduct_ambiguous_perc'] =100*(out['byproduct_ambiguous']/out['Reads_aligned_all_amplicons'])\n",
    "        out = out.fillna(0)\n",
    "\n",
    "        comb_holder.append(out)\n",
    "\n",
    "    out_dict = dict(zip(new_name, comb_holder))\n",
    "\n",
    "    return out_dict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/var/folders/5g/xxjcy2g531n02hnyb6d8qdsr0000gn/T/ipykernel_57015/3541396037.py:34: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  out['corr_perc'] = 100*(out['correct_edit']/out['Reads_aligned_all_amplicons'])\n",
      "/var/folders/5g/xxjcy2g531n02hnyb6d8qdsr0000gn/T/ipykernel_57015/3541396037.py:35: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  out['target_base_edit_perc'] = 100*(out['target_base_edit']/out['Reads_aligned_all_amplicons'])\n",
      "/var/folders/5g/xxjcy2g531n02hnyb6d8qdsr0000gn/T/ipykernel_57015/3541396037.py:36: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  out['WT_perc'] = 100*(out['WT']/out['Reads_aligned_all_amplicons'])\n",
      "/var/folders/5g/xxjcy2g531n02hnyb6d8qdsr0000gn/T/ipykernel_57015/3541396037.py:37: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  out['byproduct_all_perc'] =  100*(out['byproduct_all']/out['Reads_aligned_all_amplicons'])\n",
      "/var/folders/5g/xxjcy2g531n02hnyb6d8qdsr0000gn/T/ipykernel_57015/3541396037.py:38: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  out['byproduct_INDEL_perc'] =  100*(out['byproduct_INDEL']/out['Reads_aligned_all_amplicons'])\n",
      "/var/folders/5g/xxjcy2g531n02hnyb6d8qdsr0000gn/T/ipykernel_57015/3541396037.py:39: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  out['byproduct_sub_perc'] =  100*(out['byproduct_sub']/out['Reads_aligned_all_amplicons'])\n",
      "/var/folders/5g/xxjcy2g531n02hnyb6d8qdsr0000gn/T/ipykernel_57015/3541396037.py:40: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  out['byproduct_ambiguous_perc'] =100*(out['byproduct_ambiguous']/out['Reads_aligned_all_amplicons'])\n",
      "/var/folders/5g/xxjcy2g531n02hnyb6d8qdsr0000gn/T/ipykernel_57015/3541396037.py:34: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  out['corr_perc'] = 100*(out['correct_edit']/out['Reads_aligned_all_amplicons'])\n",
      "/var/folders/5g/xxjcy2g531n02hnyb6d8qdsr0000gn/T/ipykernel_57015/3541396037.py:35: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  out['target_base_edit_perc'] = 100*(out['target_base_edit']/out['Reads_aligned_all_amplicons'])\n",
      "/var/folders/5g/xxjcy2g531n02hnyb6d8qdsr0000gn/T/ipykernel_57015/3541396037.py:36: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  out['WT_perc'] = 100*(out['WT']/out['Reads_aligned_all_amplicons'])\n",
      "/var/folders/5g/xxjcy2g531n02hnyb6d8qdsr0000gn/T/ipykernel_57015/3541396037.py:37: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  out['byproduct_all_perc'] =  100*(out['byproduct_all']/out['Reads_aligned_all_amplicons'])\n",
      "/var/folders/5g/xxjcy2g531n02hnyb6d8qdsr0000gn/T/ipykernel_57015/3541396037.py:38: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  out['byproduct_INDEL_perc'] =  100*(out['byproduct_INDEL']/out['Reads_aligned_all_amplicons'])\n",
      "/var/folders/5g/xxjcy2g531n02hnyb6d8qdsr0000gn/T/ipykernel_57015/3541396037.py:39: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  out['byproduct_sub_perc'] =  100*(out['byproduct_sub']/out['Reads_aligned_all_amplicons'])\n",
      "/var/folders/5g/xxjcy2g531n02hnyb6d8qdsr0000gn/T/ipykernel_57015/3541396037.py:40: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  out['byproduct_ambiguous_perc'] =100*(out['byproduct_ambiguous']/out['Reads_aligned_all_amplicons'])\n"
     ]
    }
   ],
   "source": [
    "#e.g. if sample1, sample2, and sample3 were \n",
    "# from the same time-point and you wanted to merge them\n",
    "\n",
    "samps_to_merge = [['sample1', 'sample2', 'sample3'], ['sample4', 'sample5', 'sample6']]\n",
    "new_name = ['T0_MLE', 'TF_MLE']\n",
    "\n",
    "comb_dict = MLE_merge(edit_dict, samps_to_merge, new_name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Guide_ID</th>\n",
       "      <th>Reads_in_input</th>\n",
       "      <th>Reads_lowqual</th>\n",
       "      <th>Reads_aligned_all_amplicons</th>\n",
       "      <th>Reads_aligned_WT</th>\n",
       "      <th>Reads_aligned_HDR</th>\n",
       "      <th>Reads_unaligned</th>\n",
       "      <th>WT</th>\n",
       "      <th>correct_edit</th>\n",
       "      <th>target_base_edit</th>\n",
       "      <th>...</th>\n",
       "      <th>byproduct_INDEL</th>\n",
       "      <th>byproduct_sub</th>\n",
       "      <th>byproduct_ambiguous</th>\n",
       "      <th>corr_perc</th>\n",
       "      <th>target_base_edit_perc</th>\n",
       "      <th>WT_perc</th>\n",
       "      <th>byproduct_all_perc</th>\n",
       "      <th>byproduct_INDEL_perc</th>\n",
       "      <th>byproduct_sub_perc</th>\n",
       "      <th>byproduct_ambiguous_perc</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>sg_1</td>\n",
       "      <td>4002</td>\n",
       "      <td>0</td>\n",
       "      <td>4002</td>\n",
       "      <td>95</td>\n",
       "      <td>3902</td>\n",
       "      <td>5</td>\n",
       "      <td>52</td>\n",
       "      <td>286</td>\n",
       "      <td>3902</td>\n",
       "      <td>...</td>\n",
       "      <td>70</td>\n",
       "      <td>3503</td>\n",
       "      <td>91</td>\n",
       "      <td>7.146427</td>\n",
       "      <td>97.501249</td>\n",
       "      <td>1.299350</td>\n",
       "      <td>91.554223</td>\n",
       "      <td>1.749125</td>\n",
       "      <td>87.531234</td>\n",
       "      <td>2.273863</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>sg_10</td>\n",
       "      <td>6209</td>\n",
       "      <td>2</td>\n",
       "      <td>6207</td>\n",
       "      <td>2772</td>\n",
       "      <td>3429</td>\n",
       "      <td>6</td>\n",
       "      <td>68</td>\n",
       "      <td>0</td>\n",
       "      <td>3429</td>\n",
       "      <td>...</td>\n",
       "      <td>73</td>\n",
       "      <td>6007</td>\n",
       "      <td>59</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>55.244079</td>\n",
       "      <td>1.095537</td>\n",
       "      <td>98.904463</td>\n",
       "      <td>1.176092</td>\n",
       "      <td>96.777831</td>\n",
       "      <td>0.950540</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>sg_100</td>\n",
       "      <td>1453</td>\n",
       "      <td>0</td>\n",
       "      <td>1453</td>\n",
       "      <td>18</td>\n",
       "      <td>1399</td>\n",
       "      <td>36</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>1399</td>\n",
       "      <td>...</td>\n",
       "      <td>21</td>\n",
       "      <td>1372</td>\n",
       "      <td>55</td>\n",
       "      <td>0.137646</td>\n",
       "      <td>96.283551</td>\n",
       "      <td>0.206469</td>\n",
       "      <td>99.655884</td>\n",
       "      <td>1.445286</td>\n",
       "      <td>94.425327</td>\n",
       "      <td>3.785272</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>sg_1000</td>\n",
       "      <td>1153</td>\n",
       "      <td>37</td>\n",
       "      <td>1116</td>\n",
       "      <td>103</td>\n",
       "      <td>965</td>\n",
       "      <td>48</td>\n",
       "      <td>13</td>\n",
       "      <td>5</td>\n",
       "      <td>965</td>\n",
       "      <td>...</td>\n",
       "      <td>168</td>\n",
       "      <td>643</td>\n",
       "      <td>287</td>\n",
       "      <td>0.448029</td>\n",
       "      <td>86.469534</td>\n",
       "      <td>1.164875</td>\n",
       "      <td>98.387097</td>\n",
       "      <td>15.053763</td>\n",
       "      <td>57.616487</td>\n",
       "      <td>25.716846</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>sg_1001</td>\n",
       "      <td>3291</td>\n",
       "      <td>70</td>\n",
       "      <td>3221</td>\n",
       "      <td>126</td>\n",
       "      <td>3050</td>\n",
       "      <td>45</td>\n",
       "      <td>21</td>\n",
       "      <td>138</td>\n",
       "      <td>3050</td>\n",
       "      <td>...</td>\n",
       "      <td>183</td>\n",
       "      <td>2622</td>\n",
       "      <td>257</td>\n",
       "      <td>4.284384</td>\n",
       "      <td>94.691090</td>\n",
       "      <td>0.651971</td>\n",
       "      <td>95.063645</td>\n",
       "      <td>5.681465</td>\n",
       "      <td>81.403291</td>\n",
       "      <td>7.978889</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1667</th>\n",
       "      <td>sg_995</td>\n",
       "      <td>2792</td>\n",
       "      <td>106</td>\n",
       "      <td>2686</td>\n",
       "      <td>218</td>\n",
       "      <td>2362</td>\n",
       "      <td>106</td>\n",
       "      <td>131</td>\n",
       "      <td>438</td>\n",
       "      <td>2362</td>\n",
       "      <td>...</td>\n",
       "      <td>90</td>\n",
       "      <td>1726</td>\n",
       "      <td>301</td>\n",
       "      <td>16.306776</td>\n",
       "      <td>87.937453</td>\n",
       "      <td>4.877141</td>\n",
       "      <td>78.816083</td>\n",
       "      <td>3.350707</td>\n",
       "      <td>64.259121</td>\n",
       "      <td>11.206255</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1668</th>\n",
       "      <td>sg_996</td>\n",
       "      <td>7681</td>\n",
       "      <td>35</td>\n",
       "      <td>7646</td>\n",
       "      <td>266</td>\n",
       "      <td>7322</td>\n",
       "      <td>58</td>\n",
       "      <td>82</td>\n",
       "      <td>1</td>\n",
       "      <td>7322</td>\n",
       "      <td>...</td>\n",
       "      <td>38</td>\n",
       "      <td>7210</td>\n",
       "      <td>315</td>\n",
       "      <td>0.013079</td>\n",
       "      <td>95.762490</td>\n",
       "      <td>1.072456</td>\n",
       "      <td>98.914465</td>\n",
       "      <td>0.496992</td>\n",
       "      <td>94.297672</td>\n",
       "      <td>4.119801</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1669</th>\n",
       "      <td>sg_997</td>\n",
       "      <td>7577</td>\n",
       "      <td>1</td>\n",
       "      <td>7576</td>\n",
       "      <td>3661</td>\n",
       "      <td>3892</td>\n",
       "      <td>23</td>\n",
       "      <td>377</td>\n",
       "      <td>90</td>\n",
       "      <td>3892</td>\n",
       "      <td>...</td>\n",
       "      <td>76</td>\n",
       "      <td>6869</td>\n",
       "      <td>164</td>\n",
       "      <td>1.187962</td>\n",
       "      <td>51.372756</td>\n",
       "      <td>4.976241</td>\n",
       "      <td>93.835797</td>\n",
       "      <td>1.003168</td>\n",
       "      <td>90.667899</td>\n",
       "      <td>2.164731</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1670</th>\n",
       "      <td>sg_998</td>\n",
       "      <td>65</td>\n",
       "      <td>0</td>\n",
       "      <td>65</td>\n",
       "      <td>0</td>\n",
       "      <td>65</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>5</td>\n",
       "      <td>65</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>60</td>\n",
       "      <td>0</td>\n",
       "      <td>7.692308</td>\n",
       "      <td>100.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>92.307692</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>92.307692</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1671</th>\n",
       "      <td>sg_999</td>\n",
       "      <td>4296</td>\n",
       "      <td>20</td>\n",
       "      <td>4276</td>\n",
       "      <td>1429</td>\n",
       "      <td>2795</td>\n",
       "      <td>52</td>\n",
       "      <td>113</td>\n",
       "      <td>0</td>\n",
       "      <td>2795</td>\n",
       "      <td>...</td>\n",
       "      <td>4</td>\n",
       "      <td>4035</td>\n",
       "      <td>124</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>65.364827</td>\n",
       "      <td>2.642657</td>\n",
       "      <td>97.357343</td>\n",
       "      <td>0.093545</td>\n",
       "      <td>94.363891</td>\n",
       "      <td>2.899906</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1672 rows × 21 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     Guide_ID  Reads_in_input  Reads_lowqual  Reads_aligned_all_amplicons  \\\n",
       "0        sg_1            4002              0                         4002   \n",
       "1       sg_10            6209              2                         6207   \n",
       "2      sg_100            1453              0                         1453   \n",
       "3     sg_1000            1153             37                         1116   \n",
       "4     sg_1001            3291             70                         3221   \n",
       "...       ...             ...            ...                          ...   \n",
       "1667   sg_995            2792            106                         2686   \n",
       "1668   sg_996            7681             35                         7646   \n",
       "1669   sg_997            7577              1                         7576   \n",
       "1670   sg_998              65              0                           65   \n",
       "1671   sg_999            4296             20                         4276   \n",
       "\n",
       "      Reads_aligned_WT  Reads_aligned_HDR  Reads_unaligned   WT  correct_edit  \\\n",
       "0                   95               3902                5   52           286   \n",
       "1                 2772               3429                6   68             0   \n",
       "2                   18               1399               36    3             2   \n",
       "3                  103                965               48   13             5   \n",
       "4                  126               3050               45   21           138   \n",
       "...                ...                ...              ...  ...           ...   \n",
       "1667               218               2362              106  131           438   \n",
       "1668               266               7322               58   82             1   \n",
       "1669              3661               3892               23  377            90   \n",
       "1670                 0                 65                0    0             5   \n",
       "1671              1429               2795               52  113             0   \n",
       "\n",
       "      target_base_edit  ...  byproduct_INDEL  byproduct_sub  \\\n",
       "0                 3902  ...               70           3503   \n",
       "1                 3429  ...               73           6007   \n",
       "2                 1399  ...               21           1372   \n",
       "3                  965  ...              168            643   \n",
       "4                 3050  ...              183           2622   \n",
       "...                ...  ...              ...            ...   \n",
       "1667              2362  ...               90           1726   \n",
       "1668              7322  ...               38           7210   \n",
       "1669              3892  ...               76           6869   \n",
       "1670                65  ...                0             60   \n",
       "1671              2795  ...                4           4035   \n",
       "\n",
       "      byproduct_ambiguous  corr_perc  target_base_edit_perc   WT_perc  \\\n",
       "0                      91   7.146427              97.501249  1.299350   \n",
       "1                      59   0.000000              55.244079  1.095537   \n",
       "2                      55   0.137646              96.283551  0.206469   \n",
       "3                     287   0.448029              86.469534  1.164875   \n",
       "4                     257   4.284384              94.691090  0.651971   \n",
       "...                   ...        ...                    ...       ...   \n",
       "1667                  301  16.306776              87.937453  4.877141   \n",
       "1668                  315   0.013079              95.762490  1.072456   \n",
       "1669                  164   1.187962              51.372756  4.976241   \n",
       "1670                    0   7.692308             100.000000  0.000000   \n",
       "1671                  124   0.000000              65.364827  2.642657   \n",
       "\n",
       "      byproduct_all_perc  byproduct_INDEL_perc  byproduct_sub_perc  \\\n",
       "0              91.554223              1.749125           87.531234   \n",
       "1              98.904463              1.176092           96.777831   \n",
       "2              99.655884              1.445286           94.425327   \n",
       "3              98.387097             15.053763           57.616487   \n",
       "4              95.063645              5.681465           81.403291   \n",
       "...                  ...                   ...                 ...   \n",
       "1667           78.816083              3.350707           64.259121   \n",
       "1668           98.914465              0.496992           94.297672   \n",
       "1669           93.835797              1.003168           90.667899   \n",
       "1670           92.307692              0.000000           92.307692   \n",
       "1671           97.357343              0.093545           94.363891   \n",
       "\n",
       "      byproduct_ambiguous_perc  \n",
       "0                     2.273863  \n",
       "1                     0.950540  \n",
       "2                     3.785272  \n",
       "3                    25.716846  \n",
       "4                     7.978889  \n",
       "...                        ...  \n",
       "1667                 11.206255  \n",
       "1668                  4.119801  \n",
       "1669                  2.164731  \n",
       "1670                  0.000000  \n",
       "1671                  2.899906  \n",
       "\n",
       "[1672 rows x 21 columns]"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#and accessing it\n",
    "comb_dict['T0_MLE']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# MAGeCK example\n",
    "\n",
    "Example of taking the counts files and making them useable for MAGeCK. We want a table that has columns of samples and rows of guide counts."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'D24-10888-6775T_MISMATCH_PIPELINE': 'TFIN_REP1',\n",
       " 'D24-10889-6775T_MISMATCH_PIPELINE': 'TFIN_REP2',\n",
       " 'D24-10890-6775T_MISMATCH_PIPELINE': 'TFIN_REP3',\n",
       " 'D24-10891-6775T_MISMATCH_PIPELINE': 'T0_REP1',\n",
       " 'D24-10892-6775T_MISMATCH_PIPELINE': 'T0_REP2',\n",
       " 'D24-10893-6775T_MISMATCH_PIPELINE': 'T0_REP3'}"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "config_df = pd.read_csv('example_config_and_library_file/DIEGO_IDR_CONFIG_MISMATCH_PIPELINE.txt', sep=' ')\n",
    "\n",
    "\n",
    "sample_id = ['TFIN_REP1', 'TFIN_REP2', 'TFIN_REP3',\n",
    "            'T0_REP1', 'T0_REP2', 'T0_REP3',]\n",
    "\n",
    "samp = [i for i in config_df['folder_name']]\n",
    "\n",
    "samp_dict = dict(zip(samp, sample_id))\n",
    "samp_dict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Guide_ID</th>\n",
       "      <th>TFIN_REP1</th>\n",
       "      <th>TFIN_REP2</th>\n",
       "      <th>TFIN_REP3</th>\n",
       "      <th>T0_REP1</th>\n",
       "      <th>T0_REP2</th>\n",
       "      <th>T0_REP3</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>sg_1</td>\n",
       "      <td>1945.0</td>\n",
       "      <td>1721.0</td>\n",
       "      <td>2637.0</td>\n",
       "      <td>1820.0</td>\n",
       "      <td>1899.0</td>\n",
       "      <td>2317.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>sg_2</td>\n",
       "      <td>1747.0</td>\n",
       "      <td>1957.0</td>\n",
       "      <td>2777.0</td>\n",
       "      <td>4113.0</td>\n",
       "      <td>2055.0</td>\n",
       "      <td>2394.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>sg_3</td>\n",
       "      <td>1940.0</td>\n",
       "      <td>2053.0</td>\n",
       "      <td>2624.0</td>\n",
       "      <td>2573.0</td>\n",
       "      <td>1845.0</td>\n",
       "      <td>1881.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>sg_4</td>\n",
       "      <td>2791.0</td>\n",
       "      <td>3349.0</td>\n",
       "      <td>4070.0</td>\n",
       "      <td>2772.0</td>\n",
       "      <td>2515.0</td>\n",
       "      <td>3593.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>sg_5</td>\n",
       "      <td>1710.0</td>\n",
       "      <td>1874.0</td>\n",
       "      <td>1991.0</td>\n",
       "      <td>2510.0</td>\n",
       "      <td>2033.0</td>\n",
       "      <td>1912.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1667</th>\n",
       "      <td>sg_1668</td>\n",
       "      <td>150.0</td>\n",
       "      <td>176.0</td>\n",
       "      <td>180.0</td>\n",
       "      <td>218.0</td>\n",
       "      <td>17.0</td>\n",
       "      <td>43.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1668</th>\n",
       "      <td>sg_1669</td>\n",
       "      <td>234.0</td>\n",
       "      <td>249.0</td>\n",
       "      <td>441.0</td>\n",
       "      <td>237.0</td>\n",
       "      <td>78.0</td>\n",
       "      <td>808.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1669</th>\n",
       "      <td>sg_1670</td>\n",
       "      <td>168.0</td>\n",
       "      <td>397.0</td>\n",
       "      <td>202.0</td>\n",
       "      <td>486.0</td>\n",
       "      <td>169.0</td>\n",
       "      <td>727.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1670</th>\n",
       "      <td>sg_1671</td>\n",
       "      <td>159.0</td>\n",
       "      <td>274.0</td>\n",
       "      <td>214.0</td>\n",
       "      <td>68.0</td>\n",
       "      <td>600.0</td>\n",
       "      <td>249.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1671</th>\n",
       "      <td>sg_1672</td>\n",
       "      <td>65.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>29.0</td>\n",
       "      <td>8.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1672 rows × 7 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     Guide_ID  TFIN_REP1  TFIN_REP2  TFIN_REP3  T0_REP1  T0_REP2  T0_REP3\n",
       "0        sg_1     1945.0     1721.0     2637.0   1820.0   1899.0   2317.0\n",
       "1        sg_2     1747.0     1957.0     2777.0   4113.0   2055.0   2394.0\n",
       "2        sg_3     1940.0     2053.0     2624.0   2573.0   1845.0   1881.0\n",
       "3        sg_4     2791.0     3349.0     4070.0   2772.0   2515.0   3593.0\n",
       "4        sg_5     1710.0     1874.0     1991.0   2510.0   2033.0   1912.0\n",
       "...       ...        ...        ...        ...      ...      ...      ...\n",
       "1667  sg_1668      150.0      176.0      180.0    218.0     17.0     43.0\n",
       "1668  sg_1669      234.0      249.0      441.0    237.0     78.0    808.0\n",
       "1669  sg_1670      168.0      397.0      202.0    486.0    169.0    727.0\n",
       "1670  sg_1671      159.0      274.0      214.0     68.0    600.0    249.0\n",
       "1671  sg_1672       65.0        2.0       29.0      8.0      0.0      1.0\n",
       "\n",
       "[1672 rows x 7 columns]"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#-------UPDATE THESE 2 PARAMTERS----\n",
    "fp = 'example_data/counts'\n",
    "chosen_count_column = 'bc_count' #can also choose \"matched_guide_count\" or \"total_guide_count\"\n",
    "\n",
    "df_holder = []\n",
    "s_name = []\n",
    "for i, k in enumerate(samp):\n",
    "    aa = pd.read_csv(f'{fp}/{k}_count_df.csv')\n",
    "    aa = aa[['Guide_ID', chosen_count_column]]\n",
    "\n",
    "    s = samp_dict[k]\n",
    "\n",
    "    aa = aa.rename(columns = {chosen_count_column:f'{s}'})\n",
    "\n",
    "    if i>0:\n",
    "        aa = aa.rename(columns = {'Guide_ID':'Guide_ID_x'})\n",
    "    df_holder.append(aa)\n",
    "\n",
    "join_out = pd.concat(df_holder, axis=1)\n",
    "join_out = join_out[['Guide_ID'] + sample_id]\n",
    "join_out"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>sgRNA</th>\n",
       "      <th>gene</th>\n",
       "      <th>TFIN_REP1</th>\n",
       "      <th>TFIN_REP2</th>\n",
       "      <th>TFIN_REP3</th>\n",
       "      <th>T0_REP1</th>\n",
       "      <th>T0_REP2</th>\n",
       "      <th>T0_REP3</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>sg_1</td>\n",
       "      <td>ABL1</td>\n",
       "      <td>1945.0</td>\n",
       "      <td>1721.0</td>\n",
       "      <td>2637.0</td>\n",
       "      <td>1820.0</td>\n",
       "      <td>1899.0</td>\n",
       "      <td>2317.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>sg_2</td>\n",
       "      <td>ABL1</td>\n",
       "      <td>1747.0</td>\n",
       "      <td>1957.0</td>\n",
       "      <td>2777.0</td>\n",
       "      <td>4113.0</td>\n",
       "      <td>2055.0</td>\n",
       "      <td>2394.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>sg_3</td>\n",
       "      <td>ABL1</td>\n",
       "      <td>1940.0</td>\n",
       "      <td>2053.0</td>\n",
       "      <td>2624.0</td>\n",
       "      <td>2573.0</td>\n",
       "      <td>1845.0</td>\n",
       "      <td>1881.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>sg_4</td>\n",
       "      <td>ABL2</td>\n",
       "      <td>2791.0</td>\n",
       "      <td>3349.0</td>\n",
       "      <td>4070.0</td>\n",
       "      <td>2772.0</td>\n",
       "      <td>2515.0</td>\n",
       "      <td>3593.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>sg_5</td>\n",
       "      <td>AFF1</td>\n",
       "      <td>1710.0</td>\n",
       "      <td>1874.0</td>\n",
       "      <td>1991.0</td>\n",
       "      <td>2510.0</td>\n",
       "      <td>2033.0</td>\n",
       "      <td>1912.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1667</th>\n",
       "      <td>sg_1668</td>\n",
       "      <td>controlsgRNA</td>\n",
       "      <td>150.0</td>\n",
       "      <td>176.0</td>\n",
       "      <td>180.0</td>\n",
       "      <td>218.0</td>\n",
       "      <td>17.0</td>\n",
       "      <td>43.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1668</th>\n",
       "      <td>sg_1669</td>\n",
       "      <td>controlsgRNA</td>\n",
       "      <td>234.0</td>\n",
       "      <td>249.0</td>\n",
       "      <td>441.0</td>\n",
       "      <td>237.0</td>\n",
       "      <td>78.0</td>\n",
       "      <td>808.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1669</th>\n",
       "      <td>sg_1670</td>\n",
       "      <td>controlsgRNA</td>\n",
       "      <td>168.0</td>\n",
       "      <td>397.0</td>\n",
       "      <td>202.0</td>\n",
       "      <td>486.0</td>\n",
       "      <td>169.0</td>\n",
       "      <td>727.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1670</th>\n",
       "      <td>sg_1671</td>\n",
       "      <td>controlsgRNA</td>\n",
       "      <td>159.0</td>\n",
       "      <td>274.0</td>\n",
       "      <td>214.0</td>\n",
       "      <td>68.0</td>\n",
       "      <td>600.0</td>\n",
       "      <td>249.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1671</th>\n",
       "      <td>sg_1672</td>\n",
       "      <td>controlsgRNA</td>\n",
       "      <td>65.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>29.0</td>\n",
       "      <td>8.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1672 rows × 8 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "        sgRNA          gene  TFIN_REP1  TFIN_REP2  TFIN_REP3  T0_REP1  \\\n",
       "0        sg_1          ABL1     1945.0     1721.0     2637.0   1820.0   \n",
       "1        sg_2          ABL1     1747.0     1957.0     2777.0   4113.0   \n",
       "2        sg_3          ABL1     1940.0     2053.0     2624.0   2573.0   \n",
       "3        sg_4          ABL2     2791.0     3349.0     4070.0   2772.0   \n",
       "4        sg_5          AFF1     1710.0     1874.0     1991.0   2510.0   \n",
       "...       ...           ...        ...        ...        ...      ...   \n",
       "1667  sg_1668  controlsgRNA      150.0      176.0      180.0    218.0   \n",
       "1668  sg_1669  controlsgRNA      234.0      249.0      441.0    237.0   \n",
       "1669  sg_1670  controlsgRNA      168.0      397.0      202.0    486.0   \n",
       "1670  sg_1671  controlsgRNA      159.0      274.0      214.0     68.0   \n",
       "1671  sg_1672  controlsgRNA       65.0        2.0       29.0      8.0   \n",
       "\n",
       "      T0_REP2  T0_REP3  \n",
       "0      1899.0   2317.0  \n",
       "1      2055.0   2394.0  \n",
       "2      1845.0   1881.0  \n",
       "3      2515.0   3593.0  \n",
       "4      2033.0   1912.0  \n",
       "...       ...      ...  \n",
       "1667     17.0     43.0  \n",
       "1668     78.0    808.0  \n",
       "1669    169.0    727.0  \n",
       "1670    600.0    249.0  \n",
       "1671      0.0      1.0  \n",
       "\n",
       "[1672 rows x 8 columns]"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#and then add in the gene information as well\n",
    "df = pd.read_csv('example_config_and_library_file/IDR_ABE_library_columns_renamed.csv')\n",
    "guide_gene = dict(zip(df['gRNA_id'], df['Hugo_Symbol']))\n",
    "\n",
    "genes = []\n",
    "for i, val in join_out.iterrows():\n",
    "\n",
    "    k = val['Guide_ID']\n",
    "    gene = guide_gene[k]\n",
    "\n",
    "    #make all the neutral guides have the same name\n",
    "    if 'controlsgRNA' in gene:\n",
    "        gene = 'controlsgRNA'\n",
    "    genes.append(gene)\n",
    "\n",
    "join_out['gene'] = genes\n",
    "join_out = join_out.rename(columns = {'Guide_ID':'sgRNA'})\n",
    "join_out = join_out[['sgRNA', 'gene'] + sample_id]\n",
    "join_out\n",
    "\n",
    "#the first 2 column names need to be 'sgRNA' and 'gene'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "#and finally save it for running on mageck\n",
    "\n",
    "join_out.to_csv('guide_counts_IDR_ABE.txt', sep='\\t', index=False)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
